package com.server.controller;

import com.server.pojo.Admin;
import com.server.pojo.AdminLogin;
import com.server.pojo.ResponseBean;
import com.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @ClassName: LoginController
 * @Description: 登录接口
 * @Author: SongHL
 * @Date: 2025/9/13
 */
@RestController
@Api(tags = "LoginController")
public class LoginController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public ResponseBean login(AdminLogin adminLogin, HttpServletRequest request){
        return adminService.login(adminLogin.getUsername(),adminLogin.getPassword(),request);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        //判断当前登录的对象
        if(principal == null){
            return null;
        }
        String username = principal.getName();
        //通过用户名获取完整的用户对象
        Admin admin = adminService.getAdminByUsername(username);
        //不能返回用户密码
        admin.setPassword(null);
        return admin;
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public ResponseBean logout(){
        return ResponseBean.success("退出成功!");
    }
}
